package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.emun.OrderStatusEnum;
import cn.dgut.training.entity.transaction.OrderGoods;
import cn.dgut.training.entity.transaction.Orders;
import cn.dgut.training.entity.transaction.OrderCancel;
import cn.dgut.training.entity.transaction.OrderStatusTime;
import cn.dgut.training.entity.dto.OrderCancelDTO;
import cn.dgut.training.entity.dto.OrdersDTO;
import cn.dgut.training.service.IOrderCancelService;
import cn.dgut.training.service.IOrderGoodsService;
import cn.dgut.training.service.IOrderService;
import cn.dgut.training.service.IOrderStatusTimeService;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/orderCancel")
public class OrderCancelController {

    @Resource
    private IOrderCancelService orderCancelService;

    @Resource
    private IOrderGoodsService orderGoodsService;

    @Resource
    private IOrderStatusTimeService orderStatusTimeService;

    @Resource
    private IOrderService orderService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private UserService userService;

    /**
     * 用户提交退货请求
     *
     * @return
     */
    @PostMapping("/create")
    public CommonResponse createCancelOrder(@LoginUser Long userId, @RequestBody @Valid OrderCancelDTO orderCancelDTO) {

        orderCancelDTO.setUserId(userId);
        //先上传取消订单请求
        OrderCancel orderCancel = new OrderCancel();
        BeanUtils.copyProperties(orderCancelDTO, orderCancel);
        if (orderCancelService.getOne(new QueryWrapper<OrderCancel>().lambda().eq(OrderCancel::getOrderId, orderCancelDTO.getOrderId())) != null) {
            orderCancelService.updateById(orderCancel);
        } else {
            orderCancelService.save(orderCancel);
        }

        //更新订单状态-时间表
        OrderStatusTime orderStatusTime = new OrderStatusTime();
        orderStatusTime.setOrderSn(orderCancelDTO.getOrderSn());
        orderStatusTime.setOrderId(orderCancelDTO.getOrderId());
        orderStatusTime.setOrderStatus(OrderStatusEnum.APPLY_FOR_REFUND.getValue());
        orderStatusTimeService.save(orderStatusTime);

        //将订单的状态变为请求退款
        Orders orders = orderService.getById(orderCancelDTO.getOrderId());
        orders.setOrderStatus(OrderStatusEnum.APPLY_FOR_REFUND.getValue());
        orderService.updateById(orders);
        return CommonResponse.message();
    }

    ;

    /**
     * 商家确认取消订单
     *
     * @param ordersDTO{id:xxx} order表的id
     * @return
     */
    @PostMapping("/confirm")
    public CommonResponse confirmCancelOrder(@LoginUser Long userId,  @RequestBody @Valid OrdersDTO ordersDTO) {
        Orders orders = orderService.getById(ordersDTO.getId());

        //订单的状态变为取消
        orders.setOrderStatus(OrderStatusEnum.REFUND_FINISH.getValue());
        orderService.updateById(orders);

        //更新订单状态-时间表
        OrderStatusTime orderStatusTime = new OrderStatusTime();
        orderStatusTime.setOrderSn(orders.getOrderSn());
        orderStatusTime.setOrderId(orders.getId());
        orderStatusTime.setOrderStatus(OrderStatusEnum.REFUND_FINISH.getValue());
        orderStatusTimeService.save(orderStatusTime);

        //退款 giao
        userService.iDeletion(orders.getUserId(),orders.getActualPrice().toString());

        //商品库存++ gia
        List<OrderGoods> orderGoodsList=orderGoodsService.list(new QueryWrapper<OrderGoods>()
                .lambda()
                .eq(OrderGoods::getOrderId,orders.getId()));
        for(OrderGoods orderGoods:orderGoodsList){
            goodsService.updateStock(orderGoods.getProductId(),orderGoods.getNumber());
        }

        return CommonResponse.message();
    }

    /**
     * 商家拒绝取消订单
     *
     * @param  ordersDTO{id:xxx} order表的id
     * @return
     */
    @PostMapping("/refuse")
    public CommonResponse refuseCancelOrder(@LoginUser Long userId,  @RequestBody @Valid OrdersDTO ordersDTO) {
        Orders orders = orderService.getById(ordersDTO.getId());

        //订单的状态变为待评价
        orders.setOrderStatus(OrderStatusEnum.WAITING_FOR_EVALUATION.getValue());
        orderService.updateById(orders);

        //更新订单状态-时间表
        OrderStatusTime orderStatusTime = new OrderStatusTime();
        orderStatusTime.setOrderSn(orders.getOrderSn());
        orderStatusTime.setOrderId(orders.getId());
        orderStatusTime.setOrderStatus(OrderStatusEnum.WAITING_FOR_EVALUATION.getValue());
        orderStatusTimeService.save(orderStatusTime);
        return CommonResponse.message();
    }

    /**
     * 用户付款前取消订单
     *
     * @param ordersDTO{id：xxx}
     * @return
     */
    @PostMapping("/selfCancel}")
    public CommonResponse selfCancel(@LoginUser Long userId, @RequestBody @Valid OrdersDTO ordersDTO) {
        ordersDTO.setUserId(userId);
        Orders orders = orderService.getById(ordersDTO.getId());
        //订单的状态变为用户取消
        orders.setOrderStatus(OrderStatusEnum.CANCELLATION_OF_USER.getValue());
        orderService.updateById(orders);

        //更新订单状态-时间表
        OrderStatusTime orderStatusTime = new OrderStatusTime();
        orderStatusTime.setOrderSn(orders.getOrderSn());
        orderStatusTime.setOrderId(orders.getId());
        orderStatusTime.setOrderStatus(OrderStatusEnum.CANCELLATION_OF_USER.getValue());
        orderStatusTimeService.save(orderStatusTime);
        return CommonResponse.message();
    }

    /**
     *商家查看退款理由
     * @param userId
     * @param ordersDTO{订单的id id:xxx}
     * @return
     */
    @PostMapping("/detail")
    public CommonResponse selectById(@LoginUser Long userId,@RequestBody @Valid OrdersDTO ordersDTO){
        ordersDTO.setUserId(userId);
        OrderCancel orderCancel=orderCancelService.getOne(new QueryWrapper<OrderCancel>()
                .lambda()
                .eq(OrderCancel::getOrderId,ordersDTO.getId()));
        return CommonResponse.success(orderCancel);
    }

    @PostMapping("/idetail")
    public CommonResponse iselectById(@RequestBody @Valid OrdersDTO ordersDTO){
        OrderCancel orderCancel=orderCancelService.getOne(new QueryWrapper<OrderCancel>()
                .lambda()
                .eq(OrderCancel::getOrderId,ordersDTO.getId()));
        return CommonResponse.success(orderCancel);
    }
}
